// pages/utilPage/pages/index/index.js
const util = require('../../../../utils/util.js')
// pages/index/test/test.js
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    plugin: null,
    accessToken: '',
    lockId: '',
    address: '北京市北京市丰台区右安门外大街'
  },
  onLoad: function(options) {
    let self = this;
    self.setData({
      lockId: options.lockid,
      address: options.address
    })

    self.getAccessToken();
    setInterval(function() {
      self.getAccessToken();
    }, 3000 * 1000)
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var plugin = requirePlugin('DParkingLockControl');
    this.setData({
      plugin: plugin
    });
  },
  /**
   * 公共方法
   */
  downraise: function(t) {
    let self = this;
    var options = {
      toBaccessToken: self.data.accessToken,
      lockId: self.data.lockId,
      toBoper: t
    };

    self.data.plugin.DropRiseButtonHandle(
      options,
      function(data) {
        console.log('success:' + JSON.stringify(data))
      },
      function(data) {
        console.log('fail:' + JSON.stringify(data))
      },
      function(data) {
        console.log('error:' + JSON.stringify(data))
      }
    )
  },
  /**
   * 升锁
   */
  openLock: function() {
    this.downraise("rise");
  },
  /**
   * 降锁
   */
  lockLock: function() {
    this.downraise("drop");
  },
  /**
   * 获取token
   */
  getAccessToken: function() {
    let self = this;
    let url = app.globalData.apiRootUrl + '/CarLock/GetDParkingLockToken';
    app.httpRequest({
      url: url,
      showLoading: false,
      method: "GET",
      success: function(res) {
        self.setData({
          accessToken: res.data.accessToken
        });
        console.log(res.data)
      }
    });
  }
})